//
//  ViewController.m
//  UITalbeView3_7
//
//  Created by MAC on 16-3-7.
//  Copyright (c) 2016年 ___FULLUSERNAME___. All rights reserved.
//

#import "ViewController.h"
#import "CZHeroGroup.h"
@interface ViewController ()<UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

//数据模型的建立
@property(nonatomic,strong)NSArray * heroGroups;

@end

@implementation ViewController

//懒加载-延迟加载
//重写get方法
-(NSArray *)heroGroups
{
    //return _heroGroups;
    if (_heroGroups == nil) {
        //读取plist文件
        //1.获取全路径
        NSString *plistPath = [[NSBundle mainBundle]pathForResource:@"heroPlist.plist" ofType:nil];
        //NSString *plistPath2 = [[NSBundle mainBundle]pathForResource:@"heroPlist" ofType:@"plist"];
        
        //2.根据路径获取plist文件的数据
        //需要将获取的数据保存到某个对象中，该对象类型和plist文件的根节点的类型一致。
        NSArray * heros = [[NSArray alloc]initWithContentsOfFile:plistPath];
        //NSArray * heros2 = [NSArray arrayWithContentsOfFile:plistPath];
        
        //3.字典转模型
        //3.1面向模型开发，建立一个模型对象
        //3.2将plist文件中key值作为模型的属性
        NSMutableArray * tempArray = [NSMutableArray array];
        
        for (NSDictionary *dict in heros) {
            CZHeroGroup * heroGroup = [CZHeroGroup heroGroupWithDict:dict];
            [tempArray addObject:heroGroup];
        }
        _heroGroups = tempArray;
    }
    return _heroGroups;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    //设置数据源
    self.tableView.dataSource = self;
    
    
    
    //NSLog(@"%@",heros);
}

//实现数据源方法
//1.返回tableView的组数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //return 2;
    //return heros.count;
    return self.heroGroups.count;
}

//2.返回每一组的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    CZHeroGroup * heroGroup = self.heroGroups[section];
    return heroGroup.heros.count;
}

//3.给每一行设置数据
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //1.创建cell
    UITableViewCell * cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    //2.设置数据给cell
    CZHeroGroup * heroGroup = self.heroGroups[indexPath.section];
    cell.textLabel.text = heroGroup.heros[indexPath.row];
    NSLog(@"count=%d",heroGroup.heros.count);
    return cell;
}

//4.将head和footer放上去

@end
